package com.jt.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
     return    userMapper.selectList(null);
    }
/*
* 说明
* */
    @Override
    public String login(User user) {
//          1.获得原始密码
        String password =user.getPassword();
        String md5Str= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Str); //username/password
        //根据对象中不为null的属性充当where条件
        QueryWrapper<User> queryWrapper = new QueryWrapper(user);
        //4.根据条件查询数据库
        User userDB = userMapper.selectOne(queryWrapper);
        //5.定义token数据 限定条件 token不能重复
        // UUID: 随机字符串 3.4*10^38 种可能性
        String uuid = UUID.randomUUID().toString()
                .replace("-","");
        return userDB==null?null:uuid;
    }
}
